package PowerWord.Core.Data;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import PowerWord.Core.Plan.Controller;
import android.media.AudioManager;
import android.media.MediaPlayer;

public class Sound {
	static final String WebSoundPrefix = "http://www.gstatic.com/dictionary/static/sounds/lf/0/";
	static final String WebSoundSuffix = "%23_gb_1.mp3";
	static final String WebSoundPrefix2 = "http://www.gstatic.com/dictionary/static/sounds/de/0/";
	
	private RandomAccessFile soundFile = null;
	private MediaPlayer player = null;
	
	public Sound() {
		player = new MediaPlayer();
	}
	
	public void Init() {
		try {
			soundFile = new RandomAccessFile(Defines.SoundPath, "r");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void Release() {
		if(player.isPlaying()) {
			player.stop();
		}
		player.release();
		try {
			soundFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void Play(int offset, int length) {
		if(!player.isPlaying()) {
			player.reset();
			if(length != -1) {
				try {
					player.setDataSource(soundFile.getFD(), offset, length);
					player.setAudioStreamType(AudioManager.STREAM_MUSIC);
					player.prepare();
					player.start();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	public void PlaySoundNet(String word){
		String fileName = Defines.SoundDownloadDir + word + ".mp3";
		try {
			File file = new File(fileName);
			if(!file.exists() && Controller.IsWebOn()) {
				Download(word);
			}
	        player.setDataSource(fileName);
			player.setAudioStreamType(AudioManager.STREAM_MUSIC);
			player.prepare();
			player.start();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void Download(String word) {
		String newWord = word + "__";
		String keyWord = newWord.substring(0, 1) + "/" + 
						 newWord.substring(0, 2) + "/" +
						 newWord.substring(0, 3) + "/" +
						 word;
		String webSoundUrl = WebSoundPrefix + keyWord + WebSoundSuffix;
		String webSoundUrl2 = WebSoundPrefix2 + word + ".mp3";
		String fileName = Defines.SoundDownloadDir + word + ".mp3";
		try {
			DownloadFile(webSoundUrl, fileName);
		} catch (FileNotFoundException e) {
			try {
				DownloadFile(webSoundUrl2, fileName);
			} catch (IOException e2) {
				e2.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	protected void DownloadFile(String url, String file) throws MalformedURLException, IOException {
		HttpURLConnection httpUrl = (HttpURLConnection) (new URL(url)).openConnection();
        httpUrl.connect();
        BufferedInputStream bis = new BufferedInputStream(httpUrl.getInputStream());
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buf = new byte[1024*8];
        int size = 0;
        while ((size = bis.read(buf)) != -1)
            fos.write(buf, 0, size);
        fos.close();
        bis.close();
        httpUrl.disconnect();
	}
}
